//
//  SLvyouViewController.m
//  News
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 PEAK. All rights reserved.
//

#import "SLvyouViewController.h"


@interface SLvyouViewController ()<UITableViewDataSource ,UITableViewDelegate>

@property(nonatomic ,retain)UITableView *tableView;
@property(nonatomic, retain)NSArray *arr;
@property(nonatomic, retain)NSMutableArray *ArrOne;
@property(nonatomic, retain)NSMutableArray *ArrTwo;
@property(nonatomic, retain)STravelObject *travel;
@property(nonatomic, assign)NSInteger i;

@property(nonatomic ,retain)NSMutableArray *xiangqingIdZongArr;

@property(nonatomic, assign)NSInteger Pic;
//@property(nonatomic,retain)NSMutableArray *travelArr;
@property (nonatomic, retain)UIImageView *imageView;
@property (nonatomic, retain)UIActivityIndicatorView *activityIndicator;
@end

@implementation SLvyouViewController
- (void)dealloc
{
    [_tableView release];
    [_arr release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    self.title = @"旅游";
    
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
  self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui"] style:UIBarButtonItemStyleDone target:self action:@selector(fanHui)];
    
    self.ArrOne = [NSMutableArray array];
    self.ArrTwo = [NSMutableArray array];
    self.xiangqingIdZongArr = [NSMutableArray array];
    self.i = 20;
   
    
    [self create];
    
    // 请求出现小菊花
    self.imageView  = [[UIImageView alloc] initWithFrame:self.view.bounds];
    _imageView.image = [UIImage imageNamed:@"5EA2D64D-15BE-43D8-AB18-E15BCE039C8F"];
    _imageView.alpha =1;
   
    
    _activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [_activityIndicator setCenter:_imageView.center];
    _activityIndicator.backgroundColor= [UIColor grayColor];
    [_activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [_imageView addSubview:_activityIndicator];
    
    [_activityIndicator startAnimating];
 [self.view addSubview:_imageView];
 
     self.isUpLoading = NO;
    [self addHeader];
    [self addFooter];
   
}
//上拉
-(void)addHeader
{
    __block SLvyouViewController *lvyou = self;
    [self.tableView addHeaderWithCallback:^{
        lvyou.isUpLoading = NO;
        [lvyou details];
    }];
    [self.tableView headerBeginRefreshing];
}

//下拉
-(void)addFooter
{
    __block SLvyouViewController *lvyou1 = self;
    [self.tableView addFooterWithCallback:^{
        lvyou1.isUpLoading = YES;
        
        self.i = _i + 20;
        
        [lvyou1 detailsAddFooter];
        
    }];

}

-(void)fanHui
{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}


-(void)detailsAddFooter

{

    if (_i < 400) {
  

    NSString * str = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/list/T1348654204705/%ld-20.html",(long)_i];
    
    [SNetWorkHandle getDataWithURL:str completion:^(id result) {
        
        
        
        NSMutableDictionary *dic = result;
        _ArrOne = [dic objectForKey:@"T1348654204705"];
        // 重新加载数据
        if (self.isUpLoading == NO) {
            [self.ArrTwo removeAllObjects];
        }
        
        for (NSDictionary *dic in _ArrOne) {
            
            
            
            if ([[dic allKeys] containsObject:@"imgextra"] && ![[dic allKeys] containsObject:@"hasCover"]) {
                
                self.travel = [[STravelObject alloc]initWithDic:dic];
                
                [_ArrTwo addObject:_travel];
                
                
                if (_travel.skipID.length != 0) {
                    [_xiangqingIdZongArr addObject:_travel.skipID];
                    
                    
                }else if (_travel.skipID == 0){
                    
                    [_xiangqingIdZongArr addObject:_travel.docid];
                    
                }
                
                
                
            }if (![[dic allKeys] containsObject:@"imgextra"] && ![[dic allKeys] containsObject:@"hasCover"] && ![[dic allKeys] containsObject:@"TAG"]) {
                
                self.travel = [[STravelObject alloc]initWithDic:dic];
                [_ArrTwo addObject:_travel];
                
                if (_travel.skipID.length != 0) {
                    [_xiangqingIdZongArr addObject:_travel.skipID];
                    
                    
                    
                }else if (_travel.skipID == 0){
                    [_xiangqingIdZongArr addObject:_travel.docid];
                    
                }
                
            }
            
            
        }
        // 结束上拉 下拉

        [self.tableView footerEndRefreshing];
        [_tableView reloadData];
            }];
        
    }else
    {
        [self.tableView footerEndRefreshing];

    }
}


// 创建视图
-(void)create
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = 110;
    
  
    
    [self.view addSubview:_tableView];
    [_tableView release];
    
    
    
    
 
}



-(void)details
{
    
    [SNetWorkHandle getDataWithURL:@"http://c.m.163.com/nc/article/list/T1348654204705/0-20.html" completion:^(id result) {
        
       
        
        NSMutableDictionary *dic = result;
        _ArrOne = [dic objectForKey:@"T1348654204705"];
   // 重新加载数据
        if (self.isUpLoading == NO) {
            [self.ArrTwo removeAllObjects];
        }
        
        for (NSDictionary *dic in _ArrOne) {
            
            
            if ([[dic allKeys] containsObject:@"imgextra"] && ![[dic allKeys] containsObject:@"hasCover"]) {
                
                self.travel = [[STravelObject alloc]initWithDic:dic];
                
                  [_ArrTwo addObject:_travel];
                

                if (_travel.skipID.length != 0) {
                    [_xiangqingIdZongArr addObject:_travel.skipID];
                    
                    
                }else if (_travel.skipID == 0){
                    
                    [_xiangqingIdZongArr addObject:_travel.docid];
                    
                }

                
            }if (![[dic allKeys] containsObject:@"imgextra"] && ![[dic allKeys] containsObject:@"hasCover"] && ![[dic allKeys] containsObject:@"TAG"]) {
                
                self.travel = [[STravelObject alloc]initWithDic:dic];
                 [_ArrTwo addObject:_travel];
                
                if (_travel.skipID.length != 0) {
                    [_xiangqingIdZongArr addObject:_travel.skipID];
  

                }else if (_travel.skipID == 0){
                     [_xiangqingIdZongArr addObject:_travel.docid];

                }
                               
            }
            
        }
        // 结束上拉 下拉
        [self.tableView headerEndRefreshing];
    
        [_tableView reloadData];
        [_imageView removeFromSuperview];

    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    

    return _ArrTwo.count;
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    if (_Pic == 0) {
       
        STravelObject *travleOne = _ArrTwo[indexPath.row];
        
        SMyTravelTableViewCell *cell  =[tableView dequeueReusableCellWithIdentifier:@"a"];
        STravel1TableViewCell *pic1 = [tableView dequeueReusableCellWithIdentifier:@"b"];

        
    // 判断带一张图片的
        
        if (travleOne.imgextra.count == 0 && travleOne.imgsrc != 0) {
            
            if (cell == nil) {
                cell = [[SMyTravelTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"a"];
            }
            cell.STravelOne = travleOne;
            
            return  cell;
            
        }
       else if (travleOne.imgextra.count !=0)  {
            
            if (pic1 == nil) {
                pic1 = [[STravel1TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"b"];
            }
           
            pic1.STravelTwo  = travleOne;
            return pic1;
        
        }
    
  }

    
    return nil;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString * IdStr = _xiangqingIdZongArr[indexPath.row];
    
    if ([IdStr containsString:@"|"]) {
        
        SXiangqingTwoViewController * xiangping = [[SXiangqingTwoViewController alloc] init];
        xiangping.PicStr = IdStr;
        
        [self.navigationController pushViewController:xiangping animated:YES];
    }
    else if (![IdStr containsString:@"|"])
    {
        
        SXiangqingViewController * xiangping = [[SXiangqingViewController alloc] init];
        xiangping.IdStr = IdStr;
        
        
        [self. navigationController pushViewController:xiangping animated:YES];
        
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
